package com.example.innovate.service.impl;

import com.example.innovate.bean.CounterPart;
import com.example.innovate.domain.EmploymentInfoDO;
import com.example.innovate.domain.EmploymentInfoDOExample;
import com.example.innovate.mapper.EmploymentInfoDOMapper;
import com.example.innovate.service.EmploymentService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class EmploymentServiceImpl implements EmploymentService {

    @Autowired
    EmploymentInfoDOMapper employmentInfoDOMapper;

    @Override
    public EmploymentInfoDO addInfo(EmploymentInfoDO employmentInfoDO) {
        employmentInfoDOMapper.insertSelective(employmentInfoDO);
        return employmentInfoDO;
    }

    @Override
    public EmploymentInfoDO findById(int id) {
        EmploymentInfoDO employmentInfoDO = employmentInfoDOMapper.selectByPrimaryKey(id);
        return employmentInfoDO;
    }

    @Override
    public void deleteInfo(int id) {
        employmentInfoDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public EmploymentInfoDO updateInfo(EmploymentInfoDO employmentInfoDO) {
        employmentInfoDOMapper.updateByPrimaryKeySelective(employmentInfoDO);
        return employmentInfoDO;
    }

    @Override
    public List<EmploymentInfoDO> findAll(String className, String name, int userId, int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria = employmentInfoDOExample.createCriteria();

        if(className!=null){
            criteria.andEmpCreatedClassEqualTo(className);
        }
        if(name!=null){
            criteria.andEmpNameEqualTo(name);
        }
        if(userId!=0){
            criteria.andEmpUserIdEqualTo(userId);
        }
        List<EmploymentInfoDO> list = employmentInfoDOMapper.selectByExample(employmentInfoDOExample);
        return list;
    }

    @Override
    public Double proportionByMajor(String major) {
        EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
        List<EmploymentInfoDO> list1 = employmentInfoDOMapper.selectByExample(employmentInfoDOExample);
        EmploymentInfoDOExample employmentInfoDOExample1 = new EmploymentInfoDOExample();
        employmentInfoDOExample1.createCriteria()
                .andEmpMajorEqualTo(major);
        List<EmploymentInfoDO> list2 = employmentInfoDOMapper.selectByExample(employmentInfoDOExample1);
        double count = (list2.size()*1.00)/(list1.size()*1.00);
        return count;
    }

    @Override
    public CounterPart findInfo(String major) {
        EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria = employmentInfoDOExample.createCriteria();
        EmploymentInfoDOExample employmentInfoDOExample1 = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria1 = employmentInfoDOExample1.createCriteria();
        criteria1.andEmpCounterpartEqualTo(false);
        if (!StringUtils.isEmpty(major)){
            criteria.andEmpMajorEqualTo(major);
            criteria1.andEmpMajorEqualTo(major);
        }
        List<EmploymentInfoDO> list1 = employmentInfoDOMapper.selectByExample(employmentInfoDOExample);
        List<EmploymentInfoDO> list2 = employmentInfoDOMapper.selectByExample(employmentInfoDOExample1);
        CounterPart counterPart = new CounterPart();
        counterPart.setCounterPart(list1.size()-list2.size());
        counterPart.setNonCounterPart(list2.size());
        return counterPart;
    }

    @Override
    public Map<String, Integer> findCity() {
        EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria = employmentInfoDOExample.createCriteria();
        List<EmploymentInfoDO> list = employmentInfoDOMapper.selectByExample(employmentInfoDOExample);
        Set<String> set = new HashSet<>();
        for(EmploymentInfoDO employmentInfoDO:list){
            set.add(employmentInfoDO.getEmpCity());
        }
        Map<String,Integer> map = new HashMap<>();
        for(String city:set){
            EmploymentInfoDOExample employmentInfoDOExample1 = new EmploymentInfoDOExample();
            EmploymentInfoDOExample.Criteria criteria1 = employmentInfoDOExample1.createCriteria();
            criteria1.andEmpCityEqualTo(city);
            List<EmploymentInfoDO> list1 = employmentInfoDOMapper.selectByExample(employmentInfoDOExample1);
            map.put(city,list1.size());
        }
        return map;
    }

    @Override
    public Map<String, Double> findPay() {
        Map<String,Double> map = new HashMap<>();
        EmploymentInfoDOExample employmentInfo = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria = employmentInfo.createCriteria();
        double len = employmentInfoDOMapper.selectByExample(employmentInfo).size()*1.00;
        int left = 0;
        int right=2000;
        for(int i = 0;i<4; i++){
            EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
            EmploymentInfoDOExample.Criteria criteria1 = employmentInfoDOExample.createCriteria();
            if(i==3){
                criteria1.andEmpPayGreaterThanOrEqualTo(left);
                map.put(left+"-",(employmentInfoDOMapper.selectByExample(employmentInfoDOExample).size()*1.00)/len);
            }
            else {
                criteria1.andEmpPayBetween(left,right);
                map.put(left+"-"+right,(employmentInfoDOMapper.selectByExample(employmentInfoDOExample).size()*1.00)/len);
            }

            left = right+1;
            right = left+3000;
        }
        return map;
    }

    // 获取现有所有专业，不重复
    @Override
    public Set<String> findAllMajor() {
        EmploymentInfoDOExample employmentInfoDOExample = new EmploymentInfoDOExample();
        EmploymentInfoDOExample.Criteria criteria = employmentInfoDOExample.createCriteria();
        List<EmploymentInfoDO> list = employmentInfoDOMapper.selectByExample(employmentInfoDOExample);
        Set<String> set = new HashSet<>();
        for(EmploymentInfoDO employmentInfoDO:list){
            set.add(employmentInfoDO.getEmpMajor());
        }
        return set;
    }

}
